package com.jcnet.bizaia;

import java.io.File;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.bean.SPUserBean;
import com.jcnet.bizaia.bean.StateBean;
import com.jcnet.bizaia.bean.UserBean;
import com.jcnet.bizaia.tools.BBTools;
import com.jcnet.bizaia.tools.DataStorageHelper;
import com.jcnet.bizaia.tools.FileUtils;
import com.jcnet.bizaia.tools.JCTools;
import com.jcnet.bizaia.tools.Toolkit;
import com.jcnet.bizaia.view.ProgressHUD;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class UserInfoActivity extends JCSimpleSherlockActivity implements OnClickListener{

	private DataStorageHelper dataStorageHelper ;

	private TextView userNikeTextView;
	private TextView userEmailTextView;
	private TextView userPasswordTextView;

	private UserBean userBean ;

	private ImageView imageView;


	private Context context;


	private ProgressHUD progressHUD;

	private String photoData;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.jcTitle = getString(R.string.userSetting);
		super.initActionBar();
		setContentView(R.layout.user_edit_account);
		dataStorageHelper = new DataStorageHelper(UserInfoActivity.this, SPUserBean.dataTableNameForUser);

		userBean = JCTools.getUserBean(UserInfoActivity.this);

		imageView = (ImageView)findViewById(R.id.userImg);

		userEmailTextView = (TextView)findViewById(R.id.userEmail);

		userNikeTextView = (TextView)findViewById(R.id.userNikeName);

		userPasswordTextView =(TextView)findViewById(R.id.userPasssword);

		context = this;

		userEmailTextView.setText(userBean.getUsermail());
		
		System.out.println("userBean.getUserimage()+"+userBean.getUserimage());
		if(userBean!=null){
			userEmailTextView.setText(userBean.getUsermail());
			userNikeTextView.setText(userBean.getNickname());
			Picasso.with(context).load(App.HTTP_PATH+userBean.getUserimage()).centerCrop().resize(104, 104).into(imageView);
		}

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		File file = new File(BBTools.getSDPath()+"/temp.png");

		if(BBTools.isFileExit(BBTools.getSDPath()+"/temp.png")){
			BBTools.deleteFile(file);
		}
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		userNikeTextView.setText(dataStorageHelper.get(SPUserBean.nikeNameSP_2, dataStorageHelper.get(SPUserBean.regmailSP_6, "")));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.linNikeName:
			startActivity(new Intent(UserInfoActivity.this, UserEditNikeName.class));
			break;
		case R.id.linEmail:

			break;

		case R.id.linPassword:

			startActivity(new Intent(UserInfoActivity.this, UserEditPasswordActivity.class));


			break;
		case R.id.loginOutButton:
			//注销用户
			JCTools.loginOut(UserInfoActivity.this);
			Intent intent = new Intent(UserInfoActivity.this, LoginActivity.class);
			intent.putExtra("firstIn", 100);
			startActivity(intent);

			break;

		case R.id.userImg:

			File file = new File(BBTools.getSDPath()+"/temp.png");

			if(BBTools.isFileExit(BBTools.getSDPath()+"/temp.png")){
				BBTools.deleteFile(file);
			}
			
			Intent intent2 = new Intent(UserInfoActivity.this, JCSelectPicPopupWindow.class);
			startActivityForResult(intent2,BBTools.PHOTO_RESULT_CODE);


			break;


		default:
			break;
		}

	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==BBTools.PHOTO_RESULT_CODE){

			File file = new File(BBTools.getSDPath()+"/temp.png");

			if(BBTools.isFileExit(BBTools.getSDPath()+"/temp.png")){
				photoUpload();
			}else{
				imageView.setBackgroundResource(R.drawable.default_user_icon);
			}
		}
	}

	protected void photoUpload(){



		// TODO Auto-generated method stub

		if(Toolkit.isAvaliable(context)){

			try {
				if(BBTools.isFileExit(BBTools.getSDPath()+"/temp.png")){
					photoData = 	FileUtils.encodeBase64File(BBTools.getSDPath()+"/temp.png");
				}else{
					photoData = "";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


			RequestParams params = new RequestParams();
			params.put("nickname", userNikeTextView.getText().toString().trim());
			params.put("imgidNum", -1+"");
			params.put("imgurl", photoData);  //
			params.put("useridNum", dataStorageHelper.getInt(SPUserBean.userIDSP_1, -400)+"");  //用户ID


			new AsyncHttpClient().post(App.HTTP_PATH+"use_updateUserOne.action?", params, new AsyncHttpResponseHandler(){

				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					super.onStart();

					progressHUD = ProgressHUD.show(context, getText(R.string.commiting), true, true, null);
				}

				@Override
				public void onSuccess(String arg0) {
					// TODO Auto-generated method stub
					super.onSuccess(arg0);
					StateBean stateBean = new Gson().fromJson(arg0, new TypeToken<StateBean>(){}.getType());
					if(stateBean.getState()==200){
						Toast.makeText(context, getString(R.string.photoSuccess), 0).show();
						dataStorageHelper.put(SPUserBean.userImgSP_4, App.HTTP_PATH+stateBean.getMessage());
						Picasso.with(context).load(App.HTTP_PATH+stateBean.getMessage()).centerCrop().resize(104, 104).into(imageView);
					
					}else{
						Toast.makeText(context, stateBean.getMessage()+"", 0).show();
					}
				}

				@Override
				public void onFinish() {
					// TODO Auto-generated method stub
					super.onFinish();
					if(progressHUD!=null&&progressHUD.isShowing()){
						progressHUD.dismiss();

					}
				}

				@Override
				public void onFailure(Throwable arg0, String arg1) {
					// TODO Auto-generated method stub
					super.onFailure(arg0, arg1);
					if(progressHUD!=null&&progressHUD.isShowing()){
						progressHUD.dismiss();
					}

					Toast.makeText(context, getText(R.string.serverError), 0).show();
				}

			});


		}else{
			Toast.makeText(context, getText(R.string.noNetWork), 0).show();
		}



	}

}
